//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '扫描复印', //导航栏 中间的标题
    },
  },
  redirectUpload: function (event) {
    const type = event.currentTarget.dataset.type;
    if (type == 6) {
      this.chooseWxImage((url) => {
        console.log(url);
        wx.navigateTo({
          url: '/printer/upload/upload?type=6&photo=' + url
        })
      })
      // wx.chooseMessageFile({
      //   count: 1,
      //   type: 'image',
      //   success: function (res) {
      //     console.log(res);
      //     const url = res.tempFiles[0].path;
      //     wx.navigateTo({
      //       url: '/printer/upload/upload?type=' + type + '&photo=' + url
      //     })
      //   }
      // })
    } else {
      wx.navigateTo({
        url: '/printer/upload/upload?type=' + type
      })
    }
  },
  chooseWxImage: function (success) {
    let that = this;
    wx.showActionSheet({
      itemList: ['微信图片', '本地图片'],
      success: function (res) {
        const index = res.tapIndex;
        if (index === 1) {
          that.chooseUploadImage((url) => {
            success.call(this, url);
          });
        } else {
          that.chooseMessageFile((url) => {
            success.call(this, url);
          });
        }
      }
    })
  },
  chooseMessageFile: function (success) {
    wx.chooseMessageFile({
      count: 1,
      type: 'image',
      success: function (res) {
        const url = res.tempFiles[0].path;
        success.call(this, url);
      }
    })
  },
  chooseUploadImage: function (success) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      complete: (res) => {
        const tempFilePaths = res.tempFilePaths;
        success.call(this, tempFilePaths[0]);
      },
    })
  }
})
